package meijuli.com.join51.business.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jhx.common.cache.SimpleCache;
import com.jhx.common.utils.Constants;
import com.jhx.common.widget.CommonDialog;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.utils.QREncoder;

/**
 * 首页收款页面
 *
 * @author Wizong 2017-05-19 13:09
 */
public class CollectMoneyActivity extends BaseActivity {
    private Toolbar mToolbar;
    private TextView mToolbarTitle;
    private ImageView mImageQrCode;
    private CommonDialog mDialog;
    private String mCodeUrl = "http://#";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.black));
        }
        setContentView(R.layout.activity_collect_money);
        initData();
        initView();
    }

    private void initData() {
        String payMoneyUrl = SimpleCache.Companion.get(Constants.PAY_MONEY_URL);
        if (!TextUtils.isEmpty(payMoneyUrl)) {
            mCodeUrl = payMoneyUrl;
        } else {
            if(mDialog == null) {
                mDialog = new CommonDialog(this);
            }
            mDialog.setMessage("用户登录已过期请重新登录");
            mDialog.setOnClickListener(new CommonDialog.OnClickListener() {
                @Override
                public void onConfirmClick(View view) {
                    toLogin();
                }

                @Override
                public void onCancelClick(View view) {

                }
            });
            if(!mDialog.isShowing()) {
                mDialog.show();
            }
        }
    }

    private void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mImageQrCode = findViewById(R.id.image_qr_code);
        QREncoder.generateQRCodeImage(mCodeUrl, mImageQrCode, 300);
        initToolBar();
    }

    private void initToolBar() {
        mToolbar.setTitle("");
        mToolbarTitle.setText("收款");
        mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_collect_money));
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void toLogin() {
        Intent intent = new Intent(CollectMoneyActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }


}
